package com.kaiwukj.android.ufamily.mvp.ui.page.service.repairs;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpFragment;
import com.kaiwukj.android.ufamily.di.component.DaggerServiceComponent;
import com.kaiwukj.android.ufamily.di.module.ServiceModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.RepairsResult;
import com.kaiwukj.android.ufamily.mvp.ui.page.service.ServiceContract;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import butterknife.BindView;

import static com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt.RepairEveluate;

/**
 * @author Charles
 * @date 2019/8/14.
 * description：Kaiwu
 */
public class RepairsListFragment extends BaseMvpFragment<RepairsPresenter> implements ServiceContract.RepairsView {
    @BindView(R.id.rv_repairs_list)
    RecyclerView rvRepairsList;
    @BindView(R.id.smart_fl_complain_list)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.empty_view)
    QMUIEmptyView emptyView;

    private int repairId;

    private RepairsListAdapter repairsListAdapter;

    private static final int REQUEST_COMMENTS = 100;

    private int page = 1;
    private int pageSize = 12;

    private boolean isLoadingMore = false;

    private int eventPos = -1;

    public static RepairsListFragment newInstance() {
        Bundle args = new Bundle();
        RepairsListFragment fragment = new RepairsListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerServiceComponent.builder()
                .appComponent(appComponent)
                .serviceModule(new ServiceModule(this))
                .build().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_repairs_list;
    }

    @Override
    protected void initView() {

    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isLoadingMore = true;
                mPresenter.requestRepairsList(++page, pageSize);

            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.requestRepairsList(page = 1, pageSize);
            }
        });

        initRecyclerView();

        mPresenter.requestRepairsList(page = 1, pageSize);
        emptyView.show(true);
    }


    private void initRecyclerView() {

        rvRepairsList.setLayoutManager(new LinearLayoutManager(getActivity()));
        repairsListAdapter = new RepairsListAdapter();
        repairsListAdapter.setActivity(getActivity());
        rvRepairsList.setAdapter(repairsListAdapter);
        repairsListAdapter.setOnFinishListener(pos -> {
            eventPos = pos;
            repairId = repairsListAdapter.getItem(pos).getId();
            mPresenter.updateStatus(repairId);
        });
        //测试评价页面
   /*     repairsListAdapter.setOnItemClickListener((adapter, view, position) -> ARouter.getInstance()
                .build(RepairEveluate)
                .withInt("repairId", repairsListAdapter.getItem(position).getId())
                .navigation(getActivity(), REQUEST_COMMENTS));*/
    }


    @Override
    public void onTaskSuccess(int taskId) {
        super.onTaskSuccess(taskId);
        if (taskId == RepairsPresenter.COMMIT) {
            onBackPressedSupport();
        } else if (taskId == RepairsPresenter.CONFIRM_REPAIR_FINISH) {
            if (repairsListAdapter != null && eventPos > -1) {
                repairsListAdapter.confirmFinish(eventPos);
            }
            eventPos = -1;

            ARouter.getInstance()
                    .build(RepairEveluate)
                    .withInt("repairId", repairId)
                    .navigation(getActivity(), REQUEST_COMMENTS);
        }
    }

    @Override
    public void onTaskErr(int taskId) {
        super.onTaskErr(taskId);
        emptyView.hide();
        refreshLayout.finishRefresh(false);
        refreshLayout.finishLoadMore(false);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_COMMENTS) {
            if (resultCode == RESULT_OK) {
                mPresenter.requestRepairsList(page = 1, pageSize);
                // refreshLayout.autoRefresh();
                emptyView.show(true);
            }
        }
    }

    @Override
    public void onFetchRepairsList(List<RepairsResult> results) {
        emptyView.hide();
        if (isLoadingMore) {
            isLoadingMore = false;
            repairsListAdapter.addData(results);
            if (results.size() < pageSize) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishLoadMore(true);
            }
        } else {
            repairsListAdapter.setList(results);
            refreshLayout.finishRefresh(true);
        }
        if (repairsListAdapter.getItemCount() == 0) {
            emptyView.show("暂没有记录", "");
        }
    }

    @Override
    public void onGetQiNiuToken(String token) {

    }
}
